package com.ruoyi.campadmin.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.campadmin.mapper.CaItemSaleMapper;
import com.ruoyi.campadmin.service.ICaItemSaleService;
import com.ruoyi.campadmin.domain.CaItemSale;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Value;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.campadmin.domain.CaCamp;
import com.ruoyi.campadmin.service.ICaCampService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Map;

/**
 * @Description  
 * @Author kemonvvv
 * @Date 2023-06-07 
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class CaItemSaleServiceImpl implements ICaItemSaleService {


    @Resource
    private CaItemSaleMapper caItemSaleMapper;
    
    
    @Resource
    private ICaCampService caCampService;

    
    
    /**
     * 全查询
     * 
     * @param pageDomain 分页信息
     * @param caItemSale 要查询的实体类
     * @return 结果
     */
    @Override
    public Page<CaItemSale> getCaItemSalePage(PageDomain pageDomain, CaItemSale caItemSale , CaCamp caCamp) {
        if (Objects.isNull(caItemSale)) {
            return new Page<>();
        }
        try {
            LambdaQueryWrapper<CaItemSale> queryWrapper = new LambdaQueryWrapper<>(new CaItemSale());
            //if(!Objects.isNull(caItemSale.getName())){
            //	queryWrapper.like(CaItemSale::getName,caItemSale.getName());
            //}
            

            if (!Objects.isNull(caItemSale.getId())) {
                queryWrapper.eq(CaItemSale::getId, caItemSale.getId());
            }

            if (!Objects.isNull(caItemSale.getCaCampId())) {
                queryWrapper.eq(CaItemSale::getCaCampId, caItemSale.getCaCampId());
            }

            caCamp.setBaseEntityNull();
            caCamp.setId(null);
            if (!PageDomain.checkObjAllFieldsIsNull(caCamp)) {
                List<Long> idList = caCampService.getIdListByCaCamp(caCamp);
                queryWrapper.in(CaItemSale::getCaCampId, idList);
            }


            if (!Objects.isNull(caItemSale.getTitleText())) {
                queryWrapper.like(CaItemSale::getTitleText, caItemSale.getTitleText());
            }

            if (!Objects.isNull(caItemSale.getPhotoUrl())) {
                queryWrapper.like(CaItemSale::getPhotoUrl, caItemSale.getPhotoUrl());
            }

            if (!Objects.isNull(caItemSale.getPrice())) {
                queryWrapper.like(CaItemSale::getPrice, caItemSale.getPrice());
            }

            if (!Objects.isNull(caItemSale.getFreight())) {
                queryWrapper.like(CaItemSale::getFreight, caItemSale.getFreight());
            }

            if (!Objects.isNull(caItemSale.getService())) {
                queryWrapper.like(CaItemSale::getService, caItemSale.getService());
            }

            if (!Objects.isNull(caItemSale.getInventory())) {
                queryWrapper.like(CaItemSale::getInventory, caItemSale.getInventory());
            }

            if (!Objects.isNull(caItemSale.getPraiseNum())) {
                queryWrapper.like(CaItemSale::getPraiseNum, caItemSale.getPraiseNum());
            }

            if (!Objects.isNull(caItemSale.getCollectionNum())) {
                queryWrapper.like(CaItemSale::getCollectionNum, caItemSale.getCollectionNum());
            }

            if (!Objects.isNull(caItemSale.getStatus())) {
                queryWrapper.like(CaItemSale::getStatus, caItemSale.getStatus());
            }

            if (!Objects.isNull(caItemSale.getDelFlag())) {
                queryWrapper.like(CaItemSale::getDelFlag, caItemSale.getDelFlag());
            }

            if (!Objects.isNull(caItemSale.getCreateBy())) {
                queryWrapper.like(CaItemSale::getCreateBy, caItemSale.getCreateBy());
            }

            if (!Objects.isNull(caItemSale.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(caItemSale.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(caItemSale.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CaItemSale::getCreateTime, caItemSale.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(caItemSale.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CaItemSale::getCreateTime, caItemSale.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(caItemSale.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(caItemSale.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CaItemSale::getCreateTime, caItemSale.getParams().get("CreateTimeBeginTime"), caItemSale.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caItemSale.getUpdateBy())) {
                queryWrapper.like(CaItemSale::getUpdateBy, caItemSale.getUpdateBy());
            }

            if (!Objects.isNull(caItemSale.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(caItemSale.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(caItemSale.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CaItemSale::getUpdateTime, caItemSale.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(caItemSale.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CaItemSale::getUpdateTime, caItemSale.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(caItemSale.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(caItemSale.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CaItemSale::getUpdateTime, caItemSale.getParams().get("UpdateTimeBeginTime"), caItemSale.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caItemSale.getRemark())) {
                queryWrapper.like(CaItemSale::getRemark, caItemSale.getRemark());
            }
        Page<CaItemSale> caItemSaleList= caItemSaleMapper.selectPage(pageDomain.getPage(), queryWrapper);
        for(CaItemSale record:caItemSaleList.getRecords()){
            record.setCaCamp(caCampService.getCaCampById(record.getCaCampId()));
        }

            return caItemSaleList;
        } catch (Exception e) {
            e.printStackTrace();
            return new Page<>();
        }
    }
    

    
    /**
     * 查询通过id
     * 
     * @param id 实体类的主键
     * @return 结果
     */
    @Override
    public CaItemSale getCaItemSaleById(Long id) {
        if (Objects.isNull(id)) {
            return new CaItemSale();
        }
        try {
            CaItemSale caItemSale = caItemSaleMapper.selectById(id);
            caItemSale.setCaCamp(caCampService.getCaCampById(caItemSale.getCaCampId()));
            if (Objects.isNull(caItemSale)){
                caItemSale = new CaItemSale();
            }
            return caItemSale;

        } catch (Exception e) {
            e.printStackTrace();
            return new CaItemSale();
        }
    }


    
    
    /**
     * 通过类查询的属性值 id列表
     * 
     * @param caItemSale 要查询的实体类
     * @return 结果
     */
    @Override
    public List<Long> getIdListByCaItemSale(CaItemSale caItemSale) {
        if (Objects.isNull(caItemSale)) {
            return new ArrayList<>();
        }
        try {
            LambdaQueryWrapper<CaItemSale> queryWrapper = new LambdaQueryWrapper<>(new CaItemSale());
            

            if (!Objects.isNull(caItemSale.getId())) {
                queryWrapper.eq(CaItemSale::getId, caItemSale.getId());
            }

            if (!Objects.isNull(caItemSale.getCaCampId())) {
                queryWrapper.eq(CaItemSale::getCaCampId, caItemSale.getCaCampId());
            }

            if (!Objects.isNull(caItemSale.getTitleText())) {
                queryWrapper.like(CaItemSale::getTitleText, caItemSale.getTitleText());
            }

            if (!Objects.isNull(caItemSale.getPhotoUrl())) {
                queryWrapper.like(CaItemSale::getPhotoUrl, caItemSale.getPhotoUrl());
            }

            if (!Objects.isNull(caItemSale.getPrice())) {
                queryWrapper.like(CaItemSale::getPrice, caItemSale.getPrice());
            }

            if (!Objects.isNull(caItemSale.getFreight())) {
                queryWrapper.like(CaItemSale::getFreight, caItemSale.getFreight());
            }

            if (!Objects.isNull(caItemSale.getService())) {
                queryWrapper.like(CaItemSale::getService, caItemSale.getService());
            }

            if (!Objects.isNull(caItemSale.getInventory())) {
                queryWrapper.like(CaItemSale::getInventory, caItemSale.getInventory());
            }

            if (!Objects.isNull(caItemSale.getPraiseNum())) {
                queryWrapper.like(CaItemSale::getPraiseNum, caItemSale.getPraiseNum());
            }

            if (!Objects.isNull(caItemSale.getCollectionNum())) {
                queryWrapper.like(CaItemSale::getCollectionNum, caItemSale.getCollectionNum());
            }

            if (!Objects.isNull(caItemSale.getStatus())) {
                queryWrapper.like(CaItemSale::getStatus, caItemSale.getStatus());
            }

            if (!Objects.isNull(caItemSale.getDelFlag())) {
                queryWrapper.like(CaItemSale::getDelFlag, caItemSale.getDelFlag());
            }

            if (!Objects.isNull(caItemSale.getCreateBy())) {
                queryWrapper.like(CaItemSale::getCreateBy, caItemSale.getCreateBy());
            }

            if (!Objects.isNull(caItemSale.getParams().get("CreateTimeBeginTime"))||!Objects.isNull(caItemSale.getParams().get("CreateTimeEndTime"))) {
                if (Objects.isNull(caItemSale.getParams().get("CreateTimeBeginTime"))){
                    queryWrapper.lt(CaItemSale::getCreateTime, caItemSale.getParams().get("CreateTimeEndTime"));
                }
                if (Objects.isNull(caItemSale.getParams().get("CreateTimeEndTime"))){
                    queryWrapper.gt(CaItemSale::getCreateTime, caItemSale.getParams().get("CreateTimeBeginTime"));
                }
                if (!Objects.isNull(caItemSale.getParams().get("CreateTimeBeginTime")) && !Objects.isNull(caItemSale.getParams().get("CreateTimeEndTime"))) {
                    queryWrapper.between(CaItemSale::getCreateTime, caItemSale.getParams().get("CreateTimeBeginTime"), caItemSale.getParams().get("CreateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caItemSale.getUpdateBy())) {
                queryWrapper.like(CaItemSale::getUpdateBy, caItemSale.getUpdateBy());
            }

            if (!Objects.isNull(caItemSale.getParams().get("UpdateTimeBeginTime"))||!Objects.isNull(caItemSale.getParams().get("UpdateTimeEndTime"))) {
                if (Objects.isNull(caItemSale.getParams().get("UpdateTimeBeginTime"))){
                    queryWrapper.lt(CaItemSale::getUpdateTime, caItemSale.getParams().get("UpdateTimeEndTime"));
                }
                if (Objects.isNull(caItemSale.getParams().get("UpdateTimeEndTime"))){
                    queryWrapper.gt(CaItemSale::getUpdateTime, caItemSale.getParams().get("UpdateTimeBeginTime"));
                }
                if (!Objects.isNull(caItemSale.getParams().get("UpdateTimeBeginTime")) && !Objects.isNull(caItemSale.getParams().get("UpdateTimeEndTime"))) {
                    queryWrapper.between(CaItemSale::getUpdateTime, caItemSale.getParams().get("UpdateTimeBeginTime"), caItemSale.getParams().get("UpdateTimeEndTime"));
                }
            }

            if (!Objects.isNull(caItemSale.getRemark())) {
                queryWrapper.like(CaItemSale::getRemark, caItemSale.getRemark());
            }

            List<CaItemSale> caItemSales = caItemSaleMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CaItemSale caItemSaleNo:caItemSales){
                idList.add(caItemSaleNo.getId());
            }
            return idList;
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }


    /**
     * 通过caCamp外键idList查询idList
     *
     * @param caCampIdList 要查询的实体类
     * @return 结果
     **/
    public List<Long> getIdListByCaCampIdList(List<Long> caCampIdList) {
        if (Objects.isNull(caCampIdList)){
            return  new ArrayList<>();
        }
        try{
            LambdaQueryWrapper<CaItemSale> queryWrapper = new LambdaQueryWrapper<>(new CaItemSale());
            queryWrapper.in(CaItemSale::getCaCampId, caCampIdList);
            List<CaItemSale> caItemSales = caItemSaleMapper.selectList(queryWrapper);
            List<Long> idList = new ArrayList<>();
            for (CaItemSale caItemSale:caItemSales){
                idList.add(caItemSale.getId());
            }
            return idList;
        }catch (Exception e){
            return new ArrayList<>();
        }
    }

    
    /**
     * 添加
     * 
     * @param caItemSale 要添加的实体类
     * @return 结果
     */
    @Override
    public int insertCaItemSale(CaItemSale caItemSale) {
        if (Objects.isNull(caItemSale)) {
            return -1;
        }
        try {
        
            //caItemSale.setSpell(PinYin.getPinYin(caItemSale.getName()));
            return caItemSaleMapper.insert(caItemSale);
        } catch (Exception e) {
            return -1;
        }
    }

    /**
     * 更新通过id进行更新一条
     * 
     * @param caItemSale 实体类
     * @return 结果
     */
    @Override
    public int updateCaItemSaleById(CaItemSale caItemSale) {
        if (Objects.isNull(caItemSale)) {
            return -1;
        }
        try {
            //caItemSale.setSpell(PinYin.getPinYin(caItemSale.getName()));
            return caItemSaleMapper.updateById(caItemSale);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 删除通过id集合
     * 
     * @param idList id集合
     * @return 结果
     */
    @Override
    public int deleteCaItemSaleByIdList(List idList){
        if (Objects.isNull(idList)) {
            return -1;
        }
        try {
            return caItemSaleMapper.deleteBatchIds(idList);
        } catch (Exception e) {
            return -1;
        }
    }
    
    /**
     * 查询无条件全部 用于导出
     * 
     * @return 结果
     */
    @Override
    public List<CaItemSale> getCaItemSaleList() {
        try {
            LambdaQueryWrapper<CaItemSale> queryWrapper = new LambdaQueryWrapper<>(new CaItemSale());
            queryWrapper.isNotNull(CaItemSale::getId);
            return caItemSaleMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }

}
